/*
 * ScriviGrafo.java
 *
 * 
 */
package it.luigiarpino.alt2jsim.core;


import java.io.RandomAccessFile;
import java.io.File;
/**
 *
 * @author  Luigi Lorenzo Arpino 534/559
 */
public class ScriviGrafo {
    
    private Grafo grafo;
    private RandomAccessFile fileOut;
    private double maxDelay;
    private long BufferSize_in_byte;
    private long Bandwidth_in_bps;
    private boolean buildLink;
    private boolean buildNode;
    
    /** Crea una nuova istanza di ScriviGrafo*/
    public ScriviGrafo(Grafo g_) {
        this.grafo=g_;
    }
    
    
    /** Restituisce una stringa che contiene lo scenario della rete
     in linguaggio tcl*/
    public String getScenarioTxt(double maxDelay_, long BufferSize_in_byte_, long Bandwidth_in_bps_, boolean buildLink_, boolean buildNode_){
        StringBuffer buff=new StringBuffer();
        if (!buildLink_){
            maxDelay_=0;
        }
        buff.append("# File generated automatically by the program Alt2jsim\r\n\r\n");
        buff.append("mkdir drcl.comp.Component /Container\r\n");
        buff.append("cd /Container\r\n");
        buff.append("\r\n");
        buff.append("#Construction of the network\r\n");
        buff.append("puts \"Construction of the network...\"\r\n");
        buff.append("\r\n");
        for(int i=0;i<this.grafo.getNumOfNode();i++){
            buff.append("mkdir drcl.inet.Node n"+String.valueOf(i)+"\r\n");
        }
        buff.append("\r\n");
        buff.append("#Collego i nodi\r\n");
        buff.append("puts \"Sto collegando i nodi...\"\r\n");
        for(int i=0;i<this.grafo.getNumOfLink();i++){
            buff.append("\r\n");
            buff.append("mkdir drcl.inet.Link link"+String.valueOf(i)+"\r\n");
            double ritardo=maxDelay_/this.grafo.getScale()*this.grafo.getLenghtOfLink(i);
            buff.append("! link"+String.valueOf(i)+" setPropDelay "+String.valueOf(ritardo)+"\r\n");
            buff.append("connect -c n"+this.grafo.getSourceNodeOfLink(i)+"/"+i+"@ -and link"+i+"/0@\r\n");
            buff.append("connect -c n"+this.grafo.getDestNodeOfLink(i)+"/"+i+"@ -and link"+i+"/1@\r\n");
        }
        if (buildNode_){
            buff.append("\r\n");
            buff.append("#construction of the nodes\r\n");
            buff.append("puts \"Inserimento protocolli nei i nodi...\"\r\n");
            buff.append("set nodeBuilder_ [mkdir drcl.inet.NodeBuilder .nodeBuilder]\r\n");
            buff.append("$nodeBuilder_ setBufferSize "+BufferSize_in_byte_+"; #byte\r\n");
            buff.append("$nodeBuilder_ setBandwidth "+Bandwidth_in_bps_+"; #bps\r\n");
            buff.append("mkdir $nodeBuilder_/100@up\r\n");
            buff.append("$nodeBuilder_ build [! n*]\r\n");
        }
        buff.append("\r\n");
        buff.append("#impostazione nomi dei nodi\r\n");
        buff.append("puts \"Assigning names to nodes...\"\r\n");
        for(int i=0;i<this.grafo.getNumOfNode();i++){
            buff.append("! ?"+i+" setID "+this.grafo.getNameOfNode(i)+"\r\n");
        }
        buff.append("\r\n");
        buff.append("cd ..\r\n");
        buff.append("puts \"Done!\"\r\n");
        return buff.toString();
    }

    
    
    /** Restituisce una stringa che contiene lo scenario in TCL
        all'interno di html*/ 
    public String getScenarioHTML(double maxDelay_, long BufferSize_in_byte_, long Bandwidth_in_bps_, boolean buildLink_, boolean buildNode_){
        StringBuffer buff=new StringBuffer();
        if (!buildLink_){
            maxDelay_=0;
        }
        buff.append("<HTML><BODY>");
        buff.append("<FONT color='gray'># File generato automaticamente dal programma Alt2jsim</FONT><BR><BR>");
        buff.append("<FONT color='blue'>mkdir</FONT> drcl.comp.Component /Container<BR>");
        buff.append("<FONT color='blue'>cd</FONT> /Container<BR>");
        buff.append("<BR>");
        buff.append("<FONT color='gray'>#Costruzione della rete</FONT><BR>");
        buff.append("<FONT color='blue'>puts</FONT><FONT color='purple'> \"Costruisco la Rete...\" </FONT><BR>");
        buff.append("<BR>");
        for(int i=0;i<this.grafo.getNumOfNode();i++){
            buff.append("<FONT color='blue'>mkdir</FONT> drcl.inet.Node n"+String.valueOf(i)+"<BR>");
        }
        buff.append("<BR>");
        buff.append("<FONT color='gray'>#Collego i nodi</FONT><BR>");
        buff.append("<FONT color='blue'>puts </FONT> <FONT color='purple'>\"Sto collegando i nodi...\"</FONT><BR>");
        for(int i=0;i<this.grafo.getNumOfLink();i++){
            buff.append("<BR>");
            buff.append("<FONT color='blue'>mkdir</FONT> drcl.inet.Link link"+String.valueOf(i)+"<BR>");
            double ritardo=maxDelay_/this.grafo.getScale()*this.grafo.getLenghtOfLink(i);
            buff.append("<FONT color='blue'>!</FONT> link"+String.valueOf(i)+" <B>setPropDelay </B> <FONT color='purple'>"+String.valueOf(ritardo)+"</FONT><BR>");
            buff.append("<FONT color='blue'>connect</FONT> -c n"+this.grafo.getSourceNodeOfLink(i)+"/"+i+"@ -and link"+i+"/0@<BR>");
            buff.append("<FONT color='blue'>connect</FONT> -c n"+this.grafo.getDestNodeOfLink(i)+"/"+i+"@ -and link"+i+"/1@<BR>");
        }
        if (buildNode_){
            buff.append("<BR>");
            buff.append("<FONT color='gray'>#costruzione dei nodi</FONT><BR>");
            buff.append("<FONT color='blue'>puts </FONT><FONT color='purple'> \"Inserimento protocolli nei i nodi...\"</FONT><BR>");
            buff.append("<FONT color='blue'>set</FONT> nodeBuilder_ [<FONT color='blue'>mkdir</FONT> drcl.inet.NodeBuilder .nodeBuilder]<BR>");
            buff.append("$nodeBuilder_ <B>setBufferSize</B> <FONT color='purple'>"+BufferSize_in_byte_+"</FONT><FONT color='gray'>; #byte</FONT><BR>");
            buff.append("$nodeBuilder_ <B>setBandwidth</B><FONT color='purple'> "+Bandwidth_in_bps_+"</FONT><FONT color='gray'>; #bps</FONT><BR>");
            buff.append("<FONT color='blue'>mkdir</FONT> $nodeBuilder_/100@up<BR>");
            buff.append("$nodeBuilder_ <B>build</B> [<FONT color='blue'>!</FONT> n*]<BR>");
        }
        buff.append("<BR>");
        buff.append("<FONT color='gray'>#impostazione nomi dei nodi</FONT><BR>");
        buff.append("<FONT color='blue'>puts </FONT><FONT color='purple'> \"Assegnazione nomi ai nodi...\"</FONT><BR>");
        for(int i=0;i<this.grafo.getNumOfNode();i++){
            buff.append("<FONT color='blue'>!</FONT> ?"+i+" <B>setID</B> "+this.grafo.getNameOfNode(i)+"<BR>");
        }
        buff.append("<BR>");
        buff.append("<FONT color='blue'>cd</FONT> ..<BR>");
        buff.append("<FONT color='blue'>puts</FONT> <FONT color='purple'> \"Fatto!\" </FONT><BR>");
        buff.append("</BODY></HTML>");
        return buff.toString();
    }

        
    
    
    /** Scrive lo scenario  nel file indicato da nomeFileDest_ */
    public void scriviFile(String nomeFileDest_, double maxDelay_, long BufferSize_in_byte_, long Bandwidth_in_bps_, boolean buildLink_, boolean buildNode_) {
        this.maxDelay=maxDelay_;
        this.BufferSize_in_byte=BufferSize_in_byte_;
        this.Bandwidth_in_bps=Bandwidth_in_bps_;
        this.buildLink=buildLink_;
        this.buildNode=buildNode_;
        if (!this.buildLink){
            this.maxDelay=0;
        }
        try{
               File f=new File(nomeFileDest_);
               if (f.exists())
               {
            	   f.delete();
               }
               fileOut=new RandomAccessFile(f,"rw");
               fileOut.writeBytes("# File generato automaticamente dal programma Alt2jsim\r\n\r\n");
               this.WriteNetwork();
               fileOut.close();
        }
        catch(java.lang.Exception e){
            System.out.println("Impossibile scrivere nel file "+nomeFileDest_);
            System.exit(0);
        }
    }
    
    
    private void WriteNetwork() throws java.lang.Exception {    
        fileOut.writeBytes("mkdir drcl.comp.Component /Container\r\n");
        fileOut.writeBytes("cd /Container\r\n");
        fileOut.writeBytes("\r\n");
        fileOut.writeBytes("#Costruzione della rete\r\n");
        fileOut.writeBytes("puts \"Costruisco la Rete...\"\r\n");
        fileOut.writeBytes("\r\n");
        for(int i=0;i<this.grafo.getNumOfNode();i++){
            fileOut.writeBytes("mkdir drcl.inet.Node n"+String.valueOf(i)+"\r\n");
        }
        fileOut.writeBytes("\r\n");
        fileOut.writeBytes("#Collego i nodi\r\n");
        fileOut.writeBytes("puts \"Sto collegando i nodi...\"\r\n");
        for(int i=0;i<this.grafo.getNumOfLink();i++){
            fileOut.writeBytes("\r\n");
            fileOut.writeBytes("mkdir drcl.inet.Link link"+String.valueOf(i)+"\r\n");
            double ritardo=this.maxDelay/this.grafo.getScale()*this.grafo.getLenghtOfLink(i);
            fileOut.writeBytes("! link"+String.valueOf(i)+" setPropDelay "+String.valueOf(ritardo)+"\r\n");
            fileOut.writeBytes("connect -c n"+this.grafo.getSourceNodeOfLink(i)+"/"+i+"@ -and link"+i+"/0@\r\n");
            fileOut.writeBytes("connect -c n"+this.grafo.getDestNodeOfLink(i)+"/"+i+"@ -and link"+i+"/1@\r\n");
        }
        if (this.buildNode){
            fileOut.writeBytes("\r\n");
            fileOut.writeBytes("#costruzione dei nodi\r\n");
            fileOut.writeBytes("puts \"Inserimento protocolli nei i nodi...\"\r\n");
            fileOut.writeBytes("set nodeBuilder_ [mkdir drcl.inet.NodeBuilder .nodeBuilder]\r\n");
            fileOut.writeBytes("$nodeBuilder_ setBufferSize "+this.BufferSize_in_byte+"; #byte\r\n");
            fileOut.writeBytes("$nodeBuilder_ setBandwidth "+this.Bandwidth_in_bps+"; #bps\r\n");
            fileOut.writeBytes("mkdir $nodeBuilder_/100@up\r\n");
            fileOut.writeBytes("$nodeBuilder_ build [! n*]\r\n");
        }
        fileOut.writeBytes("\r\n");
        fileOut.writeBytes("#impostazione nomi dei nodi\r\n");
        fileOut.writeBytes("puts \"Assegnazione nomi ai nodi...\"\r\n");
        for(int i=0;i<this.grafo.getNumOfNode();i++){
            fileOut.writeBytes("! ?"+i+" setID "+this.grafo.getNameOfNode(i)+"\r\n");
        }
        fileOut.writeBytes("\r\n");
        fileOut.writeBytes("cd ..\r\n");
        fileOut.writeBytes("puts \"Fatto!\"\r\n");
    }
    
    
    public static void main(String arg[]){
        
    }    
}
